package com.bluedon.ism.commons.util;

import org.apache.commons.lang.time.DateUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * 日期工具类
 *
 */
public class DateUtil extends DateUtils {
	
	private static final String[] chinessDateChars = {"〇","一","二","三","四","五","六","七","八","九","十"};
	private static final String[] chinessSplit = {"年","月","日"};
	
	public static String getChinessDate(Date date){
		if(date==null) return "";
		return DateUtil.getChinessDate(DateUtil.formatToDay(date));
	}
	
	public static String getChinessDate(String date){
		if (StringUtil.isBlank(date))
            return "";
		String chinessDate = "";
		String[] parts = date.split("-");
		for (int i = 0; i < parts.length; i++) {
			String part = parts[i];
			//月日去掉0
			if(part.startsWith("0")) part = part.replace("0", "");
			for (int j = 0; j < part.length(); j++) {
				//月日大于10的在中间加上十
				if(i>0&&j==1) chinessDate+=chinessDateChars[10];
				String num = String.valueOf(part.charAt(j));
				int idx = Integer.valueOf(num);
				num = chinessDateChars[idx];
				chinessDate+=num;
			}
			if(i<chinessSplit.length)
				chinessDate+=" "+chinessSplit[i]+" ";
		}
		return chinessDate;
	}

	public static String[] getChinessDateArray(String date){
		if (StringUtil.isBlank(date))
			return new String[3];
		String[] parts = date.split("-");
		String[] arrDate = new String[parts.length];
		for (int i = 0; i < parts.length; i++) {
			String part = parts[i];
			String chinessDate = "";
			//月日去掉0
			if(part.startsWith("0")) part = part.replace("0", "");
			for (int j = 0; j < part.length(); j++) {
				//月日大于10的在中间加上十
				if(i>0&&j==1) chinessDate+=chinessDateChars[10];
				String num = String.valueOf(part.charAt(j));
				int idx = Integer.valueOf(num);
				num = chinessDateChars[idx];
				chinessDate+=num;
			}
			arrDate[i]=chinessDate;
		}
		return arrDate;
	}

	public static String getCurrentTime(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}
	
	public static String getCrurentFullTime() {
		return getCurrentTime("yyyy-MM-dd hh:mm:ss");
	}
	
	public static String getCrurentTimeStamp() {
	    return getCurrentTime("yyyyMMddhhmmss");
	}
	
	/**
	 * 格式化日期为yyyy-MM-dd hh:mm:ss
	 * @param date
	 * @return
	 */
	public static String formatToSecond(Date date) {
	    return format(date, "yyyy-MM-dd hh:mm:ss");
	}
	/**
	 * 格式化日期为yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static String formatToDay(Date date) {
	    return format(date, "yyyy-MM-dd");
	}
	
	public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
	
	/**
	 * 返回日期 yyyy-MM-dd hh:mm:ss
	 * @param date
	 * @return
	 */
	public static Date parseToSecond(String date) {
       if (StringUtil.isBlank(date))
            return null;
        return parse(date, "yyyy-MM-dd hh:mm:ss");
	}
	
	/**
	 * 返回日期 yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public static Date parseToDay(String date) {
	    if (StringUtil.isBlank(date))
	        return null;
	    return parse(date, "yyyy-MM-dd");
	}
	
	public static Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("invalid date argument for pattern " + pattern + " : " + date);
        }
	}
	
	public static void main(String[] args) {
	    System.out.println(new Date().getTime());
		System.out.println(getCrurentFullTime());
		System.out.println(getCrurentTimeStamp());
		Date date = new Date();
		System.out.println("current date " + date);
		System.out.println("10 minutes before current date " + DateUtil.addMinutes(date, -10));
		
        Timestamp timestamp = new Timestamp(new Date().getTime());
		System.out.println("timestamp " + timestamp);
		System.out.println("new Date().getTime() " + new Date().getTime());

		String[] aa =DateUtil.getChinessDateArray("2014-08-23");
		System.out.println("==="+aa[0]+"  "+aa[1]+"  "+aa[2]);
	}
}
